﻿using System;

namespace Repo.Converters
{
    public static class FieldConverter
    {
        public static string GetFieldValueAsString(object value)
        {
            if (value != null)
            {
                return value.ToString();
            }
            return null;
        }

        public static DateTime GetFieldValueAsDateTime(object value)
        {
            var dt = DateTime.MinValue;
            if (value != null && DateTime.TryParse(value.ToString(), out dt))
            {
                return dt;
            }
            return dt;
        }

        public static bool GetFieldValueAsBool(object value)
        {
            var b = false;
            if (value != null && Boolean.TryParse(value.ToString(), out b))
            {
                return b;
            }
            return b;
        }

        public static int GetFieldValueAsInt(object value)
        {
            var i = 0;
            if (value != null && int.TryParse(value.ToString(), out i))
            {
                return i;
            }
            return i;
        }
    }
}
